package com.iphone.extra.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.iphone.extra.R;

public class BatteryPercentView extends View {

    private final Paint mCircleBgPaint;
    private final Paint mCirclePercentPaint;
    private final Paint mTextPaint;

    private final int CIRCLE_WIDTH;
    private final int CIRCLE_SIZE;

    private int mLevelAngle = 2;

    public BatteryPercentView(Context context) {
        this(context, null, 0);
    }

    public BatteryPercentView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BatteryPercentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Resources res = context.getResources();

        CIRCLE_WIDTH = res.getDimensionPixelSize(R.dimen.battery_circle_width);
        CIRCLE_SIZE = res.getDimensionPixelSize(R.dimen.battery_circle_radio);

        mCircleBgPaint = new Paint();
        mCircleBgPaint.setAntiAlias(true);
        mCircleBgPaint.setDither(true);
        mCircleBgPaint.setColor(0x26000000);
        mCircleBgPaint.setStyle(Paint.Style.STROKE);
        mCircleBgPaint.setStrokeWidth(CIRCLE_WIDTH);

        mCirclePercentPaint = new Paint(mCircleBgPaint);
        mCirclePercentPaint.setColor(0xff65c466);
        mTextPaint = new Paint(mCircleBgPaint);
        mTextPaint.setStrokeWidth(res.getDimensionPixelSize(R.dimen.battery_icon_line_width));
        mTextPaint.setColor(0xff000000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int left = getPaddingLeft();
        int right = getPaddingRight();
        int top = getPaddingTop();
        int bottom = getPaddingBottom();

        int startX = CIRCLE_WIDTH / 2 + left;
        int startY = CIRCLE_WIDTH / 2 + top;
        int width = CIRCLE_SIZE * 2;
        int height = CIRCLE_SIZE * 2;

        canvas.drawCircle(CIRCLE_SIZE + startX, CIRCLE_SIZE + startY, CIRCLE_SIZE, mCircleBgPaint);
        canvas.drawArc(startX, startY, startX + width, startY + height, -90, mLevelAngle, false, mCirclePercentPaint);

        int x = (width - CIRCLE_SIZE / 2) / 2 + startX;
        int y = (height - CIRCLE_SIZE) / 2 + startY;

        canvas.drawRoundRect(x, y, x + CIRCLE_SIZE / 2, y + CIRCLE_SIZE, 6, 6, mTextPaint);
        canvas.drawLine(x + CIRCLE_SIZE / 6, y + 3, x + CIRCLE_SIZE / 3, y + 3, mTextPaint);
        canvas.drawLine(x + CIRCLE_SIZE / 6, y + CIRCLE_SIZE - 6, x + CIRCLE_SIZE / 3, y + CIRCLE_SIZE - 6, mTextPaint);
    }

    public void updateBatteryPercent(int level, ColorStateList color) {
        if (level <= 15) {
            mCirclePercentPaint.setColor(0xffee3f46);
        } else {
            mCirclePercentPaint.setColor(0xff65c466);
        }
        mLevelAngle = Math.max(2, Math.min(360, level * 360 / 100));
        mTextPaint.setColor(color.getDefaultColor());
        invalidate();
    }
}
